
#include <iostream>
#include "ParseH264.h"
#include "FileHelp.h"
#include "rtp.h"
#include <assert.h>
#include "pcapParse.h"
#include "EventLoopMgr.h"
#include "AfSocket.h"
#include "NetMgr.h"
#include "RtspServer.h"
#include "H264Source.h"
#include "G711ASource.h"

void SendFrameThread(xop::RtspServer* rtsp_server, xop::MediaSessionId session_id, int& clients);

int clients = 0;
int main()
{
#if 0
	std::shared_ptr<xop::RtspServer> oRtspServer = xop::RtspServer::Create();
	bool ret = oRtspServer->Start("0.0.0.0", 10086);
	printf("ret :%d \n", ret);


	getchar();
	while (1)
	{
		printf("########### \n");
		std::this_thread::sleep_for(std::chrono::milliseconds(40));
	}

	getchar();
	std::cout << "Hello World!\n";
#endif
	
	std::string ip = "0.0.0.0";
	std::string rtsp_url = "rtsp://127.0.0.1:8554/live";

	std::shared_ptr<xop::RtspServer> server = xop::RtspServer::Create();
	if (!server->Start(ip, 8554)) {
		return -1;
	}

#ifdef AUTH_CONFIG
	server->SetAuthConfig("-_-", "admin", "12345");
#endif

	xop::MediaSession* session = xop::MediaSession::CreateNew("live"); // url: rtsp://ip/live
	session->AddSource(xop::channel_0, xop::H264Source::CreateNew());
	//session->AddSource(xop::channel_1, xop::G711ASource::CreateNew());
	// session->startMulticast(); /* 开启组播(ip,端口随机生成), 默认使用 RTP_OVER_UDP, RTP_OVER_RTSP */

	session->AddNotifyConnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
		printf("RTSP client connect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
			clients++;
		});

	session->AddNotifyDisconnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
		printf("RTSP client disconnect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
		clients--;
		});

	std::cout << "URL: " << rtsp_url << std::endl;

	xop::MediaSessionId session_id = server->AddSession(session);
	//server->removeMeidaSession(session_id); /* 取消会话, 接口线程安全 */

	std::thread thread(SendFrameThread, server.get(), session_id, std::ref(clients));
	thread.detach();

	while (1) {
		//xop::Timer::Sleep(100);
		std::this_thread::sleep_for(std::chrono::milliseconds(100));
	}

	getchar();
	return 0;
}

void SendFrameThread(xop::RtspServer* rtsp_server, xop::MediaSessionId session_id, int& clients)
{
	ParseH264 tParseH264;
	char* tFile_264 = "1029.es";
	int ret = tParseH264.Init(tFile_264);
	if (ret != 0)
	{
		printf("ParseH264 Init error");
	}


	while (1)
	{
		if (clients > 0) /* 会话有客户端在线, 发送音视频数据 */
		{
			//printf("clients:%d \n", clients);
			{
				int len = 0;
				bool isKey = false;
				char* p = NULL;
				ret = tParseH264.GetAnnexbCompositeNALU(0, p, len, isKey);

				if (ret > 0)
				{
					xop::AVFrame videoFrame = { 0 };
					xop::VIDEO_FRAME_I;
					videoFrame.type = xop::VIDEO_FRAME_P; // 建议确定帧类型。I帧(xop::VIDEO_FRAME_I) P帧(xop::VIDEO_FRAME_P)
					if (isKey)
					{
						videoFrame.type = xop::VIDEO_FRAME_I;
					}
					videoFrame.size = len;  // 视频帧大小
					videoFrame.timestamp = xop::H264Source::GetTimestamp(); // 时间戳, 建议使用编码器提供的时间戳
					videoFrame.buffer.reset(new uint8_t[videoFrame.size]);
					memcpy(videoFrame.buffer.get(), p, videoFrame.size);

					rtsp_server->PushFrame(session_id, xop::channel_0, videoFrame); //送到服务器进行转发, 接口线程安全
				}
				

				/*
				//获取一帧 H264, 打包
				xop::AVFrame videoFrame = {0};
				videoFrame.type = 0; // 建议确定帧类型。I帧(xop::VIDEO_FRAME_I) P帧(xop::VIDEO_FRAME_P)
				videoFrame.size = video frame size;  // 视频帧大小
				videoFrame.timestamp = xop::H264Source::GetTimestamp(); // 时间戳, 建议使用编码器提供的时间戳
				videoFrame.buffer.reset(new uint8_t[videoFrame.size]);
				memcpy(videoFrame.buffer.get(), video frame data, videoFrame.size);

				rtsp_server->PushFrame(session_id, xop::channel_0, videoFrame); //送到服务器进行转发, 接口线程安全
				*/
			}

			{
				/*
				//获取一帧 AAC, 打包
				xop::AVFrame audioFrame = {0};
				audioFrame.type = xop::AUDIO_FRAME;
				audioFrame.size = audio frame size;  /* 音频帧大小
				audioFrame.timestamp = xop::AACSource::GetTimestamp(44100); // 时间戳
				audioFrame.buffer.reset(new uint8_t[audioFrame.size]);
				memcpy(audioFrame.buffer.get(), audio frame data, audioFrame.size);

				rtsp_server->PushFrame(session_id, xop::channel_1, audioFrame); // 送到服务器进行转发, 接口线程安全
				*/
			}
		}

		//xop::Timer::Sleep(1); /* 实际使用需要根据帧率计算延时! */

		std::this_thread::sleep_for(std::chrono::milliseconds(25));
	}
}
